package com.pap.product.product.agent.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.product.agent.item.dto.ItemCheckDTO;
import com.pap.product.agent.product.IProductDetailAgent;
import com.pap.product.agent.product.dto.ProductSelectedDetailsDTO;
import com.pap.product.agent.product.dto.formuladefine.ProductItemDTO;
import com.pap.product.calculate.auto.entity.ProductCalculate;
import com.pap.product.calculate.auto.mapper.ProductCalculateMapper;
import com.pap.product.item.auto.mapper.ItemMapper;
import com.pap.product.product.auto.entity.ProductDetail;
import com.pap.product.product.auto.mapper.ProductDetailMapper;

@Transactional
@Service("productDetailAgent")
public class ProductDetailAgentImpl extends PapBaseServiceImpl<ProductDetail> implements IProductDetailAgent {

	@Autowired
	private ProductDetailMapper productDetailMapper;
	
	@Autowired
	private ItemMapper itemMapper;
	
	@Autowired
	private ProductCalculateMapper productCalculateMapper;
	
	@Override
	public List<ProductSelectedDetailsDTO> selectCheckedProductItems(String productId) {
		return productDetailMapper.selectCheckedProductItems(productId);
	}

	@Override
	public List<ItemCheckDTO> selectProductDetailForList(String clientLicenseId, String productId) {
		List<ItemCheckDTO> selectfromdatabaseList = productDetailMapper.selectProductDetailForList(clientLicenseId, productId);
		// TODO 封装融资项目或者字典明细的对象数据
		return selectfromdatabaseList;
	}

	@Override
	public List<ProductItemDTO> selectProductDetailItem(String clientLicenseId, String productId) {
		List<ProductItemDTO> databaseItemDTO = productDetailMapper.selectProductDetailItem(clientLicenseId, productId);
		// 根据查询出来的融资项目，匹配对应的公式定义，把公式和计算顺序号查询出来进行匹配
		if (databaseItemDTO != null && databaseItemDTO.size() > 0) {
			for (ProductItemDTO productItemDTO : databaseItemDTO) {
				Map<Object, Object> calculateMap = new HashMap<Object, Object>();
				calculateMap.put("productId", productId);
				calculateMap.put("itemId", productItemDTO.getId());
				List<ProductCalculate> calculateList = productCalculateMapper.selectListByMap(calculateMap);
				if(calculateList != null && calculateList.size() > 0) {
					productItemDTO.setFormula(calculateList.get(0).getFormula());
					productItemDTO.setOrderNo(calculateList.get(0).getOrderNo());
				}
			}
		}
		return databaseItemDTO;
	}

}
